<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/21
 * Time: 23:07
 */
namespace app\admin\model\copy;
use think\Exception;

class CopyConfig{
    /**获取目前有的模块
     * @param string|null $path
     * @param array $array
     * @return array
     */
    static public function getDirList(string $path=null,array $array=['admin']){
        if(!$path){
            $path = '/app';
        }
        $dir =scandir($path);
        $data = [];
        foreach ($dir as $value){
            if($value!=='.'&&$value!=='..'){
                if(is_dir(rtrim($path,'/').'/'.$value)){
                    if(!in_array($value,$array)){
                        $ar = include '../app/'.$value.'/config/project.php';
                        $data[] = $ar;
                    }
                }
            }
        }
        return $data;
    }
    /**保存数据配置
     * @param string $filename 配置文件
     * @param array $data 数据
     * @return bool|int
     */
    static public function changeConfig($data=[],$filename=''){
            $str = '<?php
/**
* Created by PhpStorm.
* User: 毛文科
* Date: 2019/5/21
* Time: 13:02
*/
return ['.PHP_EOL;
            foreach ($data as $keys=>$value){
                if(is_array($value)){
                    foreach ($value as $key=>$v){
                        $str .='['.PHP_EOL;
                        if($key==count($value)-1){
                            $str.= '    "'.$key.'"=>"'.$v.'"'.PHP_EOL;
                        }else{
                            $str.= '    "'.$key.'"=>"'.$v.'",'.PHP_EOL;
                        }
                        $str .='],'.PHP_EOL;
                    }
                }else{
                    $str.= '    "'.$keys.'"=>"'.$value.'",'.PHP_EOL;
                }
            }
            $str.='];'.PHP_EOL;
        $dir = fileGetDir($filename);
        if(!is_dir($dir)){
            try{
                mkdir($dir,0777,true);
            }catch (Exception $e){
                chmod(fileGetDir($dir),0777);
                mkdir($dir,0777,true);
            }
        }
        chmod($dir,0777);
//        echo $filename;exit;
        $bool = file_put_contents($filename,$str);
        return $bool;
    }
}